package com.xyd.servlet;

import com.xyd.entity.User;
import com.xyd.service.UserService;
import com.xyd.service.impl.UserServiceImpl;
import com.xyd.util.TotalPageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类描述：
 *用户管理查看所有用户信息
 * @author 赵瑞硕
 *
 */
@WebServlet("/userVisitServlet")
public class UserVisitServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService=new UserServiceImpl();
        String updatas=request.getParameter("updatas");
        String  addname=request.getParameter("addname");
        String  add=request.getParameter("add");
        String  deas=request.getParameter("deas");
        String  currPage=request.getParameter("pageIndex");
        int pageIndex=0;
        if (currPage!=""&& currPage!=null){
            pageIndex=Integer.parseInt(currPage);
        }
        else {
            pageIndex=1;
        }
        int count=userService.getUserCount();
        TotalPageUtil totalPageUtil=new TotalPageUtil();
        int totalPages=totalPageUtil.getTotalPage(count,10);
        if (pageIndex<1){
            pageIndex=1;
        }
        if (pageIndex>totalPages){
            pageIndex=totalPages;
        }
        List<User> userList=userService.visitUser((pageIndex-1)*10,10);
        if (updatas!="" && updatas!=null){
            request.setAttribute("updatas",updatas);
        }
        if (addname!="" && addname!=null){
            request.setAttribute("addname",addname);
        }
        if (add!="" && add!=null){
            request.setAttribute("add",add);
        }
        if (deas!="" && deas!=null){
            request.setAttribute("deas",deas);
        }
        request.setAttribute("userList",userList);
        request.setAttribute("pageIndex",pageIndex);
        request.setAttribute("totalPages",totalPages);
        request.getRequestDispatcher("/manage/user.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
